﻿using System;
using DevExpress.Xpo;
using System.Web;
using System.IO;

namespace Aij1.Model
{

    public class A_Photo : XPObject
    {
        public A_Photo(Session session)
            : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here.
        }

        public override void AfterConstruction()
        {
            base.AfterConstruction();
            // Place here your initialization code.
        }

        string fTitle;
        [Size(100)]
        public string Title
        {
            get { return fTitle; }
            set { SetPropertyValue<string>("Title", ref fTitle, value); }
        }
        string fUrl;
        [Size(200)]
        public string Url
        {
            get { return fUrl; }
            set { SetPropertyValue<string>("Url", ref fUrl, value); }
        }
        string fIntro;
        [Size(500)]
        public string Intro
        {
            get { return fIntro; }
            set { SetPropertyValue<string>("Intro", ref fIntro, value); }
        }
        A_Article fAlbumId;
        [Association("A_P")]
        public A_Article AlbumId
        {
            get { return fAlbumId; }
            set { SetPropertyValue<A_Article>("AlbumId", ref fAlbumId, value); }
        }

        int fOrderId;
        public int OrderId
        {
            get { return fOrderId; }
            set { SetPropertyValue<int>("OrderId", ref fOrderId, value); }
        }
        string fSnapShotUrl;
        [Size(200)]
        public string SnapShotUrl
        {
            get { return fSnapShotUrl; }
            set { SetPropertyValue<string>("SnapShotUrl", ref fSnapShotUrl, value); }
        }

        string fImageType;
        [Size(5)]
        public string ImageType
        {
            get { return fImageType; }
            set { SetPropertyValue<string>("ImageType", ref fImageType, value); }
        }

        int fWidth;
        public int Width
        {
            get { return fWidth; }
            set { SetPropertyValue<int>("Width", ref fWidth, value); }
        }

        int fHeight;
        public int Height
        {
            get { return fHeight; }
            set { SetPropertyValue<int>("Height", ref fHeight, value); }
        }

        int fSnapShotWidth;
        public int SnapShotWidth
        {
            get { return fSnapShotWidth; }
            set { SetPropertyValue<int>("SnapShotWidth", ref fSnapShotWidth, value); }
        }

        int fSnapShotHeight;
        public int SnapShotHeight
        {
            get { return fSnapShotHeight; }
            set { SetPropertyValue<int>("SnapShotHeight", ref fSnapShotHeight, value); }
        }

        DateTime fEditTime;
        public DateTime EditTime
        {
            get { return fEditTime; }
            set { SetPropertyValue<DateTime>("EditTime", ref fEditTime, value); }
        }

        /// <summary>
        /// 是否是本站点存储的图片
        /// </summary>
        [NonPersistent]
        public bool IsLocal
        {
            get { return !String.IsNullOrEmpty(Url) && !Url.StartsWith("http://"); }
        }

        [NonPersistent]
        public bool SnapIsLocal
        {
            get { return !String.IsNullOrEmpty(SnapShotUrl) && !SnapShotUrl.StartsWith("http://"); }
        }

        protected override void OnDeleting()
        {
            base.OnDeleting();

            if (IsLocal && HttpContext.Current != null)
            {
                string imgPath = HttpContext.Current.Server.MapPath(Url);
                if (File.Exists(imgPath))
                    File.Delete(imgPath);
            }

            if (SnapIsLocal && HttpContext.Current != null)
            {
                string snapPath = HttpContext.Current.Server.MapPath(SnapShotUrl);
                if (File.Exists(snapPath))
                    File.Delete(snapPath);
            }
        }
    }

}